package io.summer.ebean.config;

import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * todo: 使用多个数据源
 * @author ml
 */
@Slf4j
@Configuration
public class EbeanDbConfig {
  @Bean
  public DatabaseConfig dbConfig(DataSource dataSource) {
    log.info("EbeanDbConfig dataSource: {}", dataSource.getClass().getName());

    DatabaseConfig config = new DatabaseConfig();
    config.loadFromProperties();
    config.setName("default");
    config.setDefaultServer(true);
    config.setDataSource(dataSource);
    config.setDdlExtra(false);

    log.info("ddlExtra: {}", config.isDdlExtra());

    return config;
  }

  @Bean
  public Database createDb(DatabaseConfig config) {
    return DatabaseFactory.create(config);
  }
}
